home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 376-400 / disk_376 / toollibrary / src / stof.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  8KB  |  355 lines

  1.  
  2.  /********************************************************************/
  3.  /****                                                            ****/
  4.  /****                                                            ****/
  5.  /****    Program          : UmwStoF UmwFtoS                      ****/
  6.  /****                                                            ****/
  7.  /****    Version          :    01.09                             ****/
  8.  /****                                                            ****/
  9.  /****    Erstversion      : 21.05.1988                           ****/
  10.  /****                                                            ****/
  11.  /****    Letzte Änderung  : 05.08.1990                           ****/
  12.  /****                                                            ****/
  13.  /****    Compiliert mit   : siehe MAKEFILE                       ****/
  14.  /****                                                            ****/
  15.  /****    Gelinkt mit      : Für Tool.Library                     ****/
  16.  /****                                                            ****/
  17.  /********************************************************************/
  18.  /****                                                            ****/
  19.  /****                                                            ****/
  20.  /****               Copyright by Rüdiger Dreier                  ****/
  21.  /****                                                            ****/
  22.  /****                                                            ****/
  23.  /********************************************************************/
  24.  
  25.  
  26.  #include "ToolProto.h"
  27.  
  28.  /* Funktion, die den linken Teil eines Strings zurückgibt */
  29.  VOID __asm left(register __a0 char *Ziel,
  30.                  register __a1 char *string,
  31.                  register __d0 LONG Anzahl)
  32.   {
  33.    int Lange;
  34.    int i;
  35.    
  36.    Lange=strlen(string);
  37.    if (Anzahl<=0 )  
  38.     {
  39.      Ziel[0]=0;
  40.      return;
  41.     }
  42.    
  43.    if (((LONG)Lange)<Anzahl)Anzahl=(LONG)Lange;
  44.    for(i=0;i<=Anzahl-1;i++)
  45.     {
  46.      Ziel[i]=string[i];
  47.     }
  48.    Ziel[i]=0;
  49.   }
  50.  
  51.  /* Funktion gibt den rechten Teil des Strings zurück */
  52.  VOID __asm right(register __a0 char *Ziel,
  53.                   register __a1 char *string,
  54.                   register __d0 LONG Anzahl)
  55.   {
  56.    int Lange;
  57.    int i;
  58.    
  59.    Lange=strlen(string);
  60.    
  61.    if (Anzahl<=0 ) 
  62.     {
  63.      Ziel[0]=0;
  64.      return;
  65.     }
  66.    
  67.    if (Lange<Anzahl)Anzahl=Lange;
  68.    
  69.    for(i=0;i<=Anzahl-1;i++)
  70.     {
  71.      Ziel[i]=string[Lange-Anzahl+i];
  72.     }
  73.    Ziel[i]=0;
  74.   }
  75.  
  76.  
  77.  /* Funktion gibt Teil aus der Mitte zurück */
  78.  VOID __asm mid(register __a0 char *Ziel,
  79.                 register __a1 char *string,
  80.                 register __d0 LONG Anfang,
  81.                 register __d1 LONG Anzahl)
  82.   {
  83.    int i;
  84.    int Lange;
  85.    
  86.    Lange=strlen(string);
  87.    
  88.    if (Anzahl<=0 ) 
  89.     {
  90.      Ziel[0]=0;
  91.      return;
  92.     }
  93.    if (Lange<Anzahl+Anfang)Anzahl=Lange-Anfang;
  94.    
  95.    for(i=Anfang;i<=Anfang+Anzahl-1;i++)
  96.     {
  97.      Ziel[i-Anfang]=string[i];
  98.     }
  99.    Ziel[i-Anfang]=0;
  100.   }
  101.  
  102.  
  103.  /* Wandelt String in DOUBLE um */
  104.  VOID __asm UmwStoF(register __a0 DOUBLE *Ziel,
  105.                     register __a1 char *string)
  106.   {
  107.    char Mant[256],Exp[50];
  108.    LONG a,Vorzeichen;
  109.    DOUBLE Manti,Expo;
  110.    DOUBLE Fehler;
  111.    
  112.    Fehler=0.0,Vorzeichen=0;
  113.    
  114.    a=stcisn(string,"ed");
  115.    if(a>=28)
  116.     {
  117.      *Ziel=Fehler;
  118.      return;
  119.     }
  120.    else
  121.     {
  122.      left(Mant,string,a);
  123.     }
  124.    
  125.    a=strlen(string)-a-1;
  126.    if(a>=8)
  127.     {
  128.      *Ziel=Fehler;
  129.      return;
  130.     }
  131.    else
  132.     {
  133.      right(Exp,string,a);
  134.     }
  135.    
  136.    if(strlen(Mant)==0 && strlen(Exp)!=0)
  137.     {
  138.      SHORT *a;
  139.      a=(SHORT *)Mant;
  140.      *a=0x3100;
  141.      /* strcpy(Mant,"1"); */
  142.     }
  143.    
  144.    a=stcisn(Exp,".");
  145.    if(a!=strlen(Exp))
  146.     {
  147.      *Ziel=Fehler;
  148.      return;
  149.     }
  150.    
  151.    Manti=stof(Mant);
  152.    Expo =stof(Exp);
  153.    if(Tst(Expo)==-1)
  154.     {
  155.      Vorzeichen=1;
  156.      Expo=Neg(Expo);
  157.     }
  158.    Expo=Pow(10.0,Expo);
  159.    if(Vorzeichen==1)
  160.     {
  161.      Expo=Div(1.0,Expo);
  162.     }
  163.    Expo =Mul(Expo,Manti);
  164.    *Ziel=Expo;
  165.   }
  166.  
  167.  
  168.  DOUBLE stof(char *string)
  169.   {
  170.    #define POS 0
  171.    #define NEG 1
  172.    
  173.    DOUBLE Wert;
  174.    DOUBLE Hilfe;
  175.    
  176.    int i,Vorz;
  177.    i=0;
  178.    Vorz=0;
  179.    Wert=0.0;
  180.    if(string[i]=='+')
  181.     {
  182.      Vorz=POS;
  183.      i++;
  184.     }
  185.    if(string[i]=='-')
  186.     {
  187.      Vorz=NEG;
  188.      i++;
  189.     }
  190.    
  191.    while(string[i]!='.' && string[i]!=0)
  192.     {
  193.      LONG val;
  194.      val=(string[i]-48L);
  195.      if(val>=0 && val <=9)
  196.       {
  197.        Wert=Mul(10.0,Wert);
  198.        Wert=Add(Flt(val),Wert);
  199.       }
  200.      i++;
  201.     }
  202.    
  203.    
  204.    if(string[i]==0)
  205.     {
  206.      if(Vorz)
  207.       {
  208.        Wert=Sub(0.0,Wert);
  209.       }
  210.      return(Wert);
  211.     }
  212.    
  213.    i++;
  214.    
  215.    Hilfe=.1;
  216.    
  217.    while(string[i]!=0)
  218.     {
  219.      Wert=Add(Wert, Mul(Hilfe,Flt(((string[i]-48L)))));
  220.      Hilfe=Div(Hilfe,10.0);
  221.      i++;
  222.     }
  223.    if(Vorz)
  224.     {
  225.      Wert=Sub(0.0,Wert);
  226.     }
  227.    return(Wert);
  228.   }
  229.  
  230.  extern struct Library *MathIeeeDoubBasBase;
  231.  extern struct Library *MathIeeeDoubTransBase;
  232.  
  233.  VOID ftos(char *Ziel,
  234.            DOUBLE Zahl,
  235.            LONG Nachkomma)
  236.   {
  237.    /* Erhält Zahl ohne Vorzeichen und <10 */
  238.    DOUBLE V;
  239.    LONG i;
  240.    
  241.    for (i=1;i<=Nachkomma+1;i++)
  242.     {
  243.      V=Vorkomma(Zahl);
  244.      Zahl=Sub(Zahl,V);
  245.      Ziel[i-1]=48+Fix(V);
  246.      Zahl=Mul(Zahl,10.0);
  247.     }
  248.    Ziel[i-1]=0;
  249.   }
  250.  
  251.  VOID __asm UmwFtoS(register __a0 char *Ziel,
  252.                     register __a1 DOUBLE *Zahl1,
  253.                     register __d0 LONG Nachkomma)
  254.   {
  255.    DOUBLE Lange,Runden,Zahl;
  256.    LONG Vorzeichen;
  257.    char string[50],ZW[50];
  258.    char ZW1[50];
  259.    
  260.    Zahl=*Zahl1;
  261.    
  262.    if(Vorzeichen=Tst(Zahl))
  263.     {
  264.      Zahl=Div(Zahl,Flt(Vorzeichen));                   /* Zahl positiv   */
  265.      if(Cmp(Zahl,.9)==1)Zahl=Add(Zahl,.00000001);      /* Runden für int */
  266.      if(Cmp(Zahl,1e6)==1L)Zahl=Add(Zahl,1.0);          /* Runden für int */
  267.      
  268.      Lange=Floor(Log10(Zahl));           /* Anzahl Vorkommastellen       */
  269.      Zahl=Div(Zahl,Pow(10.0,Lange));     /* Nur noch 1 Stelle vorm Komma */
  270.      if(Cmp(Lange,3.0)<=0 && Cmp(Lange,-2.0)>=0)
  271.       {
  272.        Runden=Pow(10.0,Add(Flt(Nachkomma),Lange));
  273.        Runden=Div(0.500001,Runden);
  274.        Zahl=Add(Zahl,Runden);
  275.        /* Durch Runden kann eine Stelle mehr entstanden sein, z.B. 0.99999999 */
  276.        Runden=Add(Floor(Log10(Zahl)),.0001);   /* Anzahl Vorkommastellen */
  277.        if(Cmp(Runden,1.000000)==1)
  278.         {
  279.          Lange=Add(Lange,1.0000);
  280.          Zahl=Div(Zahl,10.0);
  281.         }
  282.        ftos(string,Zahl,Nachkomma+Fix(Lange));
  283.        
  284.        left(ZW1,string,Fix(Lange)+1);
  285.        strcpy(ZW,ZW1);                         /* Vorkommateil  kopieren */
  286.        strcat(ZW,".");
  287.        if(!Cmp(Lange,-2.0))strcat(ZW,"0");
  288.        
  289.        right(ZW1,string,Nachkomma);
  290.        strcat(ZW,ZW1);                         /* Nachkommateil kopieren */
  291.       }
  292.      else
  293.       {
  294.        LONG Vorzeichen;
  295.        Runden=Pow(10.0,Flt(Nachkomma));
  296.        Runden=Div(0.5001,Runden);
  297.        Zahl=Add(Zahl,Runden);
  298.        
  299.        ftos(string,Zahl,Nachkomma);
  300.        
  301.        left(ZW,string,1);          /* Vorkommateil  kopieren */
  302.        strcat(ZW,".");
  303.        
  304.        right(ZW1,string,Nachkomma);
  305.        strcat(ZW,ZW1); /* Nachkommateil kopieren */
  306.        strcat(ZW,"d");
  307.        Zahl=Lange;
  308.        
  309.        if(Vorzeichen=Tst(Zahl))
  310.         {
  311.          Zahl=Div(Zahl,Flt(Vorzeichen));  /* Exponent posivit     */
  312.         }
  313.        if(Cmp(Zahl,.9)==1)Zahl=Add(Zahl,.0000000001); /* Runden für int */
  314.        Runden=Floor(Log10(Zahl));
  315.        Nachkomma=Fix(Runden); /* Nachkomma enthält den Exponenten */
  316.        
  317.        Lange=Floor(Log10(Zahl));          /* Länge des Exponenten */
  318.        Zahl=Div(Zahl,Pow(10.0,Lange));
  319.        ftos(string,Zahl,Nachkomma);
  320.        if(Vorzeichen==-1)strcat(ZW,"-");
  321.        strcat(ZW,string);
  322.       }
  323.      if(Vorzeichen==-1)
  324.       {
  325.        SHORT *a;
  326.        a=(SHORT *)string;
  327.        *a=0x2d00;
  328.        /* strcpy(string,"-"); */
  329.        strcat(string,ZW);
  330.       }
  331.      else
  332.       {
  333.        strcpy(string,ZW);
  334.       }
  335.     }
  336.    else
  337.     {
  338.      SHORT *a;
  339.      a=(SHORT *)string;
  340.      *a=0x3000;
  341.      /*strcpy(string,"0");*/
  342.     }
  343.    strcpy(Ziel,string);
  344.   }
  345.  
  346.  DOUBLE Vorkomma(DOUBLE Zahl)
  347.   {
  348.    LONG Int;
  349.    
  350.    Int=Fix(Zahl);
  351.    Zahl=Flt(Int);
  352.    return(Zahl);
  353.   }
  354.  
  355.